package main
import (
	"fmt"
	"errors"
)
//非环形队列

//用一个结构体来管理队列
type SingleQueue struct{
	maxSize int
	array [5]int
	front int
	rear int
}

//添加数据到队列
func (this *SingleQueue)AddQueue(value int)(err error){
	//判断队列是否已满？也就是判断rear=最后的元素
	if this.rear == this.maxSize - 1{
		return errors.New("queue full~")
	}
	this.rear++ //rear 后移
	this.array[this.rear] = value
	return nil
}
//显示队列 
func (this *SingleQueue)ShowQueue(){
	fmt.Println("显示队列：")
	for i:= this.front+1;i <= this.rear;i++{
		fmt.Printf("array[%d]=%d\t",i,this.array[i])
	}
}
//从队列中取出数据
func (this *SingleQueue)GetQueue() (val int,err error){
	if this.rear == this.front {
		return -1,errors.New("queue empty~")
	}
	this.front++
	val = this.array[this.front]
	return val,nil
}
func main(){
	//先创建一个队列
	squeue := &SingleQueue{
		maxSize : 5,
		front : -1,
		rear : -1,
	}
	squeue.AddQueue(11)
	squeue.AddQueue(22)
	squeue.AddQueue(33)
	squeue.ShowQueue()
	fmt.Printf("front:%d-rear:%d\n",squeue.front,squeue.rear)
	val,err := squeue.GetQueue()
	if err != nil{
		fmt.Println(err)
	}
	fmt.Println("Get the val is :",val)
	squeue.ShowQueue()	
	fmt.Printf("front:%d-rear:%d\n",squeue.front,squeue.rear)
	val,err = squeue.GetQueue()
	val,err = squeue.GetQueue()
	val,err = squeue.GetQueue()
	if err != nil{
		fmt.Println(err)
	}
}